import bcrypt from "bcrypt";
import { userCreateSchema } from "@/validators/user";
export default defineEventHandler(async (event) => {
  const result = await runValidate(userCreateSchema, event);
  const config = useRuntimeConfig();
  const { account, password } = result.data;
  const user = await UserSchema.findOne({ account: account }).lean();
  if (user) {
    throw createError({
      statusCode: 409,
      statusMessage: "该账号存在，请修改后重试",
    });
  }
  const hash = await bcrypt.hash(password, config.bcrypt.saltRounds);
  const userCreatedData = result.data;

  userCreatedData.password = hash;

  const newUser = await UserSchema.create(userCreatedData);
  return newUser;
});
